package org.dyndns.cooland.indexer;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.*;

public class OrbiterAddon {
	String name;
	String author;
	String version;
	String license;
	String category;
	ArrayList compatibility = new ArrayList();
	String description;
	ArrayList required = new ArrayList();;
	ArrayList optional = new ArrayList();
	Element sourceXML = null;
	
	public OrbiterAddon (Element xml){
		sourceXML=xml;
		parseXML(xml);
	}
	
	
	//<tag>text</tag>
	private String getElementText (Element xml, String tag){
		NodeList lst = xml.getElementsByTagName(tag);
		return lst.item(0).getTextContent().trim();
	}
	//<tag>
	//<key>text<key>
	//<key>text</key>
	//</tag>
	private ArrayList getArrayText(Element xml, String tag, String key){
		ArrayList lst = new ArrayList();
		NodeList nlst=xml.getElementsByTagName(tag);
		Node node=nlst.item(0);
		NodeList keylst = ((Element)node).getElementsByTagName(key);
		for (int  i=0;i<keylst.getLength();i++){
			lst.add(keylst.item(i).getTextContent().trim());			
		}
		return lst;
	}
	//<key attr1="" attr2=""></key>
	private HashMap getAttributes(Element xml){
		HashMap map = new HashMap();
				NamedNodeMap nnm=xml.getAttributes();
				for (int i=0;i<nnm.getLength();i++){
				Node cur = nnm.item(i);
				String name = cur.getNodeName();
				String attrib=cur.getNodeValue();
				map.put(name, attrib);
				
				}
		return map;
	}
	
	public void parseXML (Element xml){
		name = getElementText(xml,"name");
		author = getElementText(xml,"author");
		version = getElementText(xml,"version");
		license = getElementText(xml,"license");
		category = getElementText(xml,"category");
		description = getElementText(xml,"description");
		compatibility = getArrayText(xml,"compatibility","build");
		
		NodeList lst=xml.getElementsByTagName("required");
		Node node=lst.item(0);
		NodeList lst1=((Element)node).getElementsByTagName("addon");
		for (int i=0;i<lst1.getLength();i++){
			required.add(getAttributes((Element)lst1.item(i)));
		}
		
		NodeList lst_opt=xml.getElementsByTagName("optional");
		Node node_opt=lst_opt.item(0);
		NodeList lst1_opt=((Element)node_opt).getElementsByTagName("addon");
		for (int i=0;i<lst1_opt.getLength();i++){
			optional.add(getAttributes((Element)lst1_opt.item(i)));
		}
		
	}
	
	public Element getXML(){
		return sourceXML;
	} 
	
	public String getName() {
		return name;
	}
	/*public void setName(String name) {
		this.name = name;
	}*/
	public String getAuthor() {
		return author;
	}
	/*public void setAuthor(String author) {
		this.author = author;
	}*/
	public String getVersion() {
		return version;
	}
	/*public void setVersion(String version) {
		this.version = version;
	}*/
	public String getLicense() {
		return license;
	}
	/*public void setLicense(String license) {
		this.license = license;
	}*/
	public String getCategory() {
		return category;
	}
	/*public void setCategory(String category) {
		this.category = category;
	}*/
	public ArrayList getCompatibility() {
		return compatibility;
	}
	/*public void setCompatibility(ArrayList compatibility) {
		this.compatibility = compatibility;
	}*/
	public String getDescription() {
		return description;
	}
	/*public void setDescription(String description) {
		this.description = description;
	}*/
	public ArrayList getRequired() {
		return required;
	}
	/*public void setRequired(ArrayList required) {
		this.required = required;
	}*/
	public ArrayList getOptional() {
		return optional;
	}
	
	/*public void setOptional(ArrayList optional) {
		this.optional = optional;
	}
	*/
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Name: "+name+"\n");
		sb.append("Author: "+author+"\n");
		sb.append("Version: "+version+"\n");
		sb.append("License: "+license+"\n");
		sb.append("Category: "+category+"\n");
		sb.append("Description: "+description+"\n");
		sb.append("Compatibility: ");
		for (int i=0;i<compatibility.size();i++){
			sb.append(compatibility.get(i)+" ");
		}
		sb.append("\n");
		sb.append("Required ("+required.size()+"):\n");
			for (int i=0;i<required.size();i++){
				HashMap map=(HashMap)required.get(i);
						sb.append("\tname="+map.get("name")+" version="+map.get("version")+"\n");
					
			}
		sb.append("Optional ("+optional.size()+"):\n");
			for (int i=0;i<optional.size();i++){
				HashMap map=(HashMap)optional.get(i);
						sb.append("\tname="+map.get("name")+" version="+map.get("version")+"\n");
					
			}
			
		return sb.toString();
	}
}
